package com.kingdee.pda.ui.query_replenish_order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.adapter.ReplenishOrderAdapter;
import com.kingdee.pda.entity.ReplenishOrderBean;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.util.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 补货单查询
 * Created by dlj
 * DATE 2021/6/23
 * TIME 17:20
 */
public class QueryReplenishOrderActivity extends BaseTitleActivity implements View.OnKeyListener, QueryReplenishOrderContract.IQueryOrderView {

    private ListView mListView;
    private ReplenishOrderAdapter mAdapter;
    private TabLayout tabLayout;
    private String[] titles = new String[]{"全部", "待补货", "补货中", "补货完成"};
    private int selectPosition;
    private List<ReplenishOrderBean.DataBean> data = new ArrayList<>();
    private EditText et_orderNo;
    private QueryRepleOrderPresenter queryRepleOrderPresenter;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, QueryReplenishOrderActivity.class));
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_query_replenish_order);

        queryRepleOrderPresenter = new QueryRepleOrderPresenter(this, this);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("补货单查询");
        mListView = findViewById(R.id.list_view);
        tabLayout = findViewById(R.id.tabLayout);
        et_orderNo = findViewById(R.id.et_orderNo);

        setEditTextScannerEvents(et_orderNo, this);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                selectPosition = tab.getPosition();
                if (selectPosition == 0) {
                    queryRepleOrderPresenter.queryReplenishOrderList(postionChangetoSatus(), "", getCurrentWarehouse().getId(), 500, 1);
                } else if (selectPosition == 1) {
                    queryRepleOrderPresenter.queryReplenishOrderList(postionChangetoSatus(), "", getCurrentWarehouse().getId(), 500, 1);
                } else if (selectPosition == 2) {
                    queryRepleOrderPresenter.queryReplenishOrderList(postionChangetoSatus(), "", getCurrentWarehouse().getId(), 500, 1);
                } else {
                    queryRepleOrderPresenter.queryReplenishOrderList(postionChangetoSatus(), "", getCurrentWarehouse().getId(), 500, 1);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ReplenishOrderBean.DataBean dataBean = mAdapter.getData().get(i);
                Intent intent = new Intent(QueryReplenishOrderActivity.this, ReplenishOrderDetailActivity.class);
                intent.putExtra("billNo", dataBean.getBillNo());
                startActivity(intent);
            }
        });

        findViewById(R.id.btn_query).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        mAdapter = new ReplenishOrderAdapter(this);
        mListView.setAdapter(mAdapter);

        for (int i = 0; i < titles.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_query:
                String orderNo = et_orderNo.getText().toString().trim();
                if (TextUtils.isEmpty(orderNo)) {
                    ToastUtils.showLong(this, "请输入要查询的SKU编码/补货单号！");
                } else {
                    queryRepleOrderPresenter.queryReplenishOrderList(postionChangetoSatus(), orderNo, getCurrentWarehouse().getId(), 500, 1);
                }
                break;
        }
    }

    private String postionChangetoSatus() {
        String status = "";
        if (selectPosition == 0) {
            status = "A,B,C,D,E,F,G";
        } else if (selectPosition == 1) {
            status = "D";
        } else if (selectPosition == 2) {
            status = "E";
        } else {
            status = "F";
        }
        return status;
    }

    private void initNoDataView() {
        mListView.setVisibility(View.GONE);
    }

    private void showListView(List<ReplenishOrderBean.DataBean> list) {
        if (list != null && list.size() > 0) {
            mListView.setVisibility(View.VISIBLE);
            mAdapter.setData(list);
        } else {
            initNoDataView();
            SpeakUtils.speakWithToast(QueryReplenishOrderActivity.this, "暂无数据", true, true);
        }
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {
        queryRepleOrderPresenter.queryReplenishOrderList(postionChangetoSatus(), "", getCurrentWarehouse().getId(), 500, 1);
    }

    @Override
    protected void onResume() {
        super.onResume();

        tabLayout.getTabAt(0).select();
        et_orderNo.setText("");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if ((keyCode==KeyEvent.KEYCODE_SYSRQ||keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            if (v.getId() == R.id.et_orderNo) {
                queryRepleOrderPresenter.queryReplenishOrderList(postionChangetoSatus(), input, getCurrentWarehouse().getId(), 500, 1);
                return true;
            }
        }
        return false;
    }

    @Override
    public void onGetOrderSuccess(List<ReplenishOrderBean.DataBean> list) {
        //按状态、时间进行排序
        Collections.sort(list, new Comparator<ReplenishOrderBean.DataBean>() {
            public int compare(ReplenishOrderBean.DataBean e1, ReplenishOrderBean.DataBean e2) {
                int a = e1.getBillStatus().compareTo(e2.getBillStatus());
                if (a != 0) {
                    return a > 0 ? 1 : -1;
                }
                a = e2.getModifyDate().compareTo(e1.getModifyDate());
                return a > 0 ? 1 : -1;
            }
        });
        dismissLoadingDialog();
        data.clear();
        data.addAll(list);
        showListView(data);
    }

    @Override
    public void onGetOrderFailed(String msg) {
        initNoDataView();
        SpeakUtils.speakWithToast(QueryReplenishOrderActivity.this, msg, false, false);
    }

}
